#include <iostream>
#include <cstddef>
#include <cstdint>
#include <bitset>
#include <iomanip>
#include <vector>
#include <algorithm>

void demonstrate_byte() {
  std::cout << "  C++17 std::byte 演示:\n\n";

  // 1. 基本用法
  std::cout << "  1. 基本用法:\n";

  // 使用初始化列表构造
  std::byte b1{0x42};
  std::byte b2{0xFF};
  std::byte b3{0x00};

  std::cout << "    b1 = 0x" << std::hex << std::setfill('0') << std::setw(2)
            << static_cast<unsigned>(b1) << "\n";
  std::cout << "    b2 = 0x" << std::setw(2) << static_cast<unsigned>(b2) << "\n";
  std::cout << "    b3 = 0x" << std::setw(2) << static_cast<unsigned>(b3) << "\n";

  // 2. 位运算操作
  std::cout << "\n  2. 位运算操作:\n";

  std::byte mask = std::byte{0xF0};  // 11110000
  std::byte value = std::byte{0xAB}; // 10101011

  std::cout << "    掩码: 0x" << std::setw(2) << static_cast<unsigned>(mask) << "\n";
  std::cout << "    值:   0x" << std::setw(2) << static_cast<unsigned>(value) << "\n";

  std::byte result_and = value & mask;
  std::byte result_or = value | mask;
  std::byte result_xor = value ^ mask;
  std::byte result_not = ~value;
  std::byte result_left = value << 2;
  std::byte result_right = value >> 2;

  std::cout << "    AND (&):  0x" << std::setw(2) << static_cast<unsigned>(result_and) << "\n";
  std::cout << "    OR (|):   0x" << std::setw(2) << static_cast<unsigned>(result_or) << "\n";
  std::cout << "    XOR (^):  0x" << std::setw(2) << static_cast<unsigned>(result_xor) << "\n";
  std::cout << "    NOT (~):  0x" << std::setw(2) << static_cast<unsigned>(result_not) << "\n";
  std::cout << "    << 2:     0x" << std::setw(2) << static_cast<unsigned>(result_left) << "\n";
  std::cout << "    >> 2:     0x" << std::setw(2) << static_cast<unsigned>(result_right) << "\n";

  // 3. 复合赋值运算符
  std::cout << "\n  3. 复合赋值运算符:\n";

  std::byte b = std::byte{0x0F};
  std::cout << "    初始值: 0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  b &= std::byte{0xF0};
  std::cout << "    &= 0xF0: 0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  b = std::byte{0x0F};
  b |= std::byte{0xF0};
  std::cout << "    |= 0xF0: 0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  b = std::byte{0x0F};
  b ^= std::byte{0xFF};
  std::cout << "    ^= 0xFF: 0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  b = std::byte{0x0F};
  b <<= 2;
  std::cout << "    <<= 2:   0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  b = std::byte{0xF0};
  b >>= 2;
  std::cout << "    >>= 2:   0x" << std::setw(2) << static_cast<unsigned>(b) << "\n";

  // 4. 与整数类型的转换
  std::cout << "\n  4. 与整数类型的转换:\n";

  // 从整数构造
  std::byte from_int{42};
  std::cout << "    从int构造: 0x" << std::setw(2) << static_cast<unsigned>(from_int) << "\n";

  // 转换为整数（必须显式转换）
  unsigned char uc = static_cast<unsigned char>(from_int);
  int i = static_cast<int>(from_int);
  std::cout << "    转换为unsigned char: " << static_cast<unsigned>(uc) << "\n";
  std::cout << "    转换为int: " << i << "\n";

  // 5. 字节数组操作
  std::cout << "\n  5. 字节数组操作:\n";

  std::vector<std::byte> byte_array = {
    std::byte{0x48}, std::byte{0x65}, std::byte{0x6C}, std::byte{0x6C}, std::byte{0x6F}
  };

  std::cout << "    字节数组: ";
  for (std::byte byte_val : byte_array) {
    std::cout << "0x" << std::setw(2) << static_cast<unsigned>(byte_val) << " ";
  }
  std::cout << "\n";

  // 6. 实际应用：位掩码操作
  std::cout << "\n  6. 实际应用 - 位掩码操作:\n";

  // 定义标志位
  constexpr std::byte FLAG_A = std::byte{0x01};  // 00000001
  constexpr std::byte FLAG_B = std::byte{0x02};  // 00000010
  constexpr std::byte FLAG_C = std::byte{0x04};  // 00000100
  constexpr std::byte FLAG_D = std::byte{0x08};  // 00001000

  std::byte flags = FLAG_A | FLAG_C;  // 设置A和C标志

  std::cout << "    标志值: 0x" << std::setw(2) << static_cast<unsigned>(flags) << "\n";

  // 检查标志
  if ((flags & FLAG_A) != std::byte{0}) {
    std::cout << "    标志A已设置\n";
  }
  if ((flags & FLAG_B) == std::byte{0}) {
    std::cout << "    标志B未设置\n";
  }

  // 设置标志
  flags |= FLAG_B;
  std::cout << "    设置标志B后: 0x" << std::setw(2) << static_cast<unsigned>(flags) << "\n";

  // 清除标志
  flags &= ~FLAG_C;
  std::cout << "    清除标志C后: 0x" << std::setw(2) << static_cast<unsigned>(flags) << "\n";

  // 7. 类型安全性
  std::cout << "\n  7. 类型安全性:\n";

  std::cout << "    std::byte 的优势:\n";
  std::cout << "    - 类型安全，不能隐式转换为整数\n";
  std::cout << "    - 明确表示正在处理原始字节数据\n";
  std::cout << "    - 避免意外进行算术运算\n";
  std::cout << "    - 只能进行位运算，语义更清晰\n";

  // 8. 与C风格数组的交互
  std::cout << "\n  8. 与C风格数组的交互:\n";

  unsigned char c_array[] = {0xDE, 0xAD, 0xBE, 0xEF};
  std::byte* byte_ptr = reinterpret_cast<std::byte*>(c_array);

  std::cout << "    C数组转换为byte指针: ";
  for (size_t i = 0; i < 4; ++i) {
    std::cout << "0x" << std::setw(2) << static_cast<unsigned>(byte_ptr[i]) << " ";
  }
  std::cout << "\n";

  // 9. 字节序操作示例
  std::cout << "\n  9. 字节序操作示例:\n";

  // 模拟读取little-endian的32位整数
  std::byte little_endian_bytes[] = {
    std::byte{0xEF}, std::byte{0xBE}, std::byte{0xAD}, std::byte{0xDE}
  };

  std::cout << "    Little-endian字节序列: ";
  for (const auto& byte_val : little_endian_bytes) {
    std::cout << "0x" << std::setw(2) << static_cast<unsigned>(byte_val) << " ";
  }
  std::cout << "\n";

  // 转换为整数（little-endian）
  uint32_t int_value = 0;
  for (size_t i = 0; i < 4; ++i) {
    int_value |= static_cast<uint32_t>(little_endian_bytes[i]) << (i * 8);
  }
  std::cout << "    转换后的32位整数: 0x" << std::hex << int_value << std::dec << "\n";
}

